﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Features;
using PloobsEngine.Commands;

namespace PloobsEngine.DataAquisition
    {
       

        public class GraphDataSaver : DataHandlers
        {

            public GraphDataSaver(EngineStuff engine)
            {
                if (!engine.HasComponentAdded(PloobsConsole.MyName))
                {
                    engine.AddComponent(new PloobsConsole(engine));
                }
                this.engine = engine;
            }                 

            #region DataHandlers Members
            private EngineStuff engine;
            private Dictionary<String, Stopwatch> hand = new Dictionary<string, Stopwatch>();
            private Dictionary<String, PloobsConsoleParametersCommand> commands = new Dictionary<string, PloobsConsoleParametersCommand>();
            private Dictionary<String, List<double>> values = new Dictionary<String, List<double>>();
            private Dictionary<FpsType, PloobsConsoleParametersCommand> fpsCaptured = new Dictionary<FpsType, PloobsConsoleParametersCommand>();

            int i = 0;
            public void HandleData(DataFormat data)
            {
                if (data is MethodDataFormat)
                {
                    MethodDataFormat dat = data as MethodDataFormat;
                    if (dat.Part == MethodPart.START)
                    {
                        if (!hand.ContainsKey(dat.MethodName))
                        {
                            PloobsConsoleParametersCommand pcp = new PloobsConsoleParametersCommand();
                            pcp.createVar(dat.MethodName, new GraphVar(engine, dat.MethodName, (Quadrant)i++, 100, 1.5f, 0));
                            commands.Add(dat.MethodName, pcp);

                            Stopwatch sw = new Stopwatch();
                            sw.Start();
                            hand[dat.MethodName] = sw;
                            values[dat.MethodName] = new List<double>();
                        }
                        else
                        {
                            hand[dat.MethodName].Start();
                        }
                    }
                    else
                    {
                        if (hand.ContainsKey(dat.MethodName))
                        {
                            Stopwatch old = hand[dat.MethodName];
                            old.Stop();

                            commands[dat.MethodName].setAtribute(dat.MethodName, (float)old.Elapsed.TotalMilliseconds);
                            CommandProcessor.getCommandProcessor().SendCommandSyncronous(commands[dat.MethodName]);

                            old.Reset();
                        }
                    }
                }

                else if( data is FpsDataFormat )
                {
                    FpsDataFormat df = data as FpsDataFormat;
                    if(fpsCaptured.ContainsKey(df.FpsType))
                    {
                        fpsCaptured[df.FpsType].setAtribute(df.FpsTypeString, df.Fps);
                        CommandProcessor.getCommandProcessor().SendCommandSyncronous(fpsCaptured[df.FpsType]);

                    }
                    else
                    {
                        PloobsConsoleParametersCommand pcp = new PloobsConsoleParametersCommand();
                        pcp.createVar(df.FpsTypeString, new GraphVar(engine, df.FpsTypeString, (Quadrant)i++, 25, 70, 0));
                        fpsCaptured.Add(df.FpsType, pcp);
                        CommandProcessor.getCommandProcessor().SendCommandSyncronous(pcp);

                    }
                }
            }

            public Type[] GetHandledType
            {
                get { return new Type[] { typeof(MethodDataFormat), typeof(FpsDataFormat) }; }
            }

            #endregion
        }
    }
